﻿<%@ WebHandler Language="C#" Class="AdminHandle" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Text;
using TaoSH.AppCode;
using TaoSH.Data;


/// <summary>
/// Summary description for LoginHandler
/// </summary>
public class AdminHandle : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        context.Response.ContentType = "text/plain";
        string strAction = context.Request.QueryString["Action"].ToString();

        try
        {

            switch (strAction)
            {
                case "UpdateMM"://获取首页中的菜单index.aspx
                    {
                        string strUserName = HttpContext.Current.Session["UserName"] as string;
                        string strPassword = context.Request.Form["NPASD1"] as string;
                        new JH_Auth_UserB().UpadteMiMa(strUserName, strPassword);
                    }
                    break;
                case "GetMainMenu"://获取首页中的菜单index.aspx
                    {
                        string strUserName = context.Request.QueryString["UserName"].ToString();
                        string strPMenuCode = context.Request.QueryString["PMenuCode"] ?? "17";
                        string strMenu = "";
                        DataTable ListAuth = new JH_Auth_MenuB().GetDTByCommand(" select * from vw_JH_Auth_UserAuth where  UserName='" + strUserName + "'  and isMenu='Y' and IsUse='Y'");
                        strMenu = new JH_Auth_MenuB().GetChiMenuByUserName(strUserName, int.Parse(strPMenuCode), ListAuth);
                        context.Response.Write(strMenu);
                    }
                    break;
                case "addRole"://添加角色Role.aspx
                    {
                        JH_Auth_Role NewRole = new JH_Auth_Role();
                        NewRole.PRoleCode = 0;
                        NewRole.RoleName = context.Request.QueryString["rolename"].ToString();
                        NewRole.RoleDec = context.Request.QueryString["roledec"].ToString();
                        NewRole.isSysRole = context.Request.QueryString["issys"].ToString();
                        NewRole.IsUse = context.Request.QueryString["isuse"].ToString();
                        new JH_Auth_RoleB().Insert(NewRole);
                        context.Response.Write("success");
                    }
                    break;
                case "GetRole"://获取所有角色Role.aspx
                    {
                        string strRoleTree = "[" + new JH_Auth_RoleB().GetRoleTree(-1).TrimEnd(',') + "]";
                        context.Response.Write(strRoleTree);
                    }
                    break;
                case "delRole"://删除角色Role.aspx
                    {
                        int intRoleCode = int.Parse(context.Request.QueryString["delCode"].ToString());
                        new JH_Auth_RoleB().delRole(intRoleCode);
                    }
                    break;


                case "AddAuth"://删除角色Role.aspx
                    {
                        StringBuilder strAddAuthTree = new StringBuilder();
                        JH_Auth_Menu NewAuth = new JH_Auth_Menu();
                        NewAuth.PAuthCode = int.Parse(context.Request.QueryString["nodeid"].ToString());
                        NewAuth.DisplayOrder = int.Parse(context.Request.QueryString["nodeorder"].ToString());
                        NewAuth.AuthLevel = int.Parse(context.Request.QueryString["nodelever"].ToString()) + 1;
                        NewAuth.AuthName = context.Request.QueryString["nodename"].ToString();
                        NewAuth.FileName = context.Request.QueryString["nodeurl"].ToString();
                        NewAuth.IsUse = context.Request.QueryString["nodeisuse"].ToString();
                        NewAuth.Ico = context.Request.QueryString["nodeico"].ToString();
                        NewAuth.isMenu = "Y";
                        NewAuth.isSys = "N";
                        NewAuth.AuthDesc = "";
                        new JH_Auth_MenuB().Insert(NewAuth);
                        strAddAuthTree.AppendFormat("{{id:'{0}',pId:'{1}',level:'{2}',name:'{3}',taburl:'{4}',isuse:'{5}',icon:'{6}'}}", NewAuth.AuthCode, NewAuth.PAuthCode, NewAuth.AuthLevel, NewAuth.AuthName, NewAuth.FileName, NewAuth.IsUse, NewAuth.Ico);
                        context.Response.Write("[" + strAddAuthTree + "]");

                    }
                    break;
                case "UpdateAuth"://
                    {
                        int nodeid = int.Parse(context.Request.QueryString["nodeid"].ToString());
                        StringBuilder strUpAuthTree = new StringBuilder();
                        JH_Auth_Menu UpAuth = new JH_Auth_MenuB().GetEntities(d => d.AuthCode == nodeid).SingleOrDefault();
                        UpAuth.AuthName = context.Request.QueryString["nodename"].ToString();
                        UpAuth.DisplayOrder = int.Parse(context.Request.QueryString["nodeorder"].ToString());
                        UpAuth.FileName = context.Request.QueryString["nodeurl"].ToString();
                        UpAuth.IsUse = context.Request.QueryString["nodeisuse"].ToString();
                        UpAuth.Ico = context.Request.QueryString["nodeico"].ToString();
                        new JH_Auth_MenuB().Update(UpAuth);
                        strUpAuthTree.AppendFormat("{{id:'{0}',pId:'{1}',level:'{2}',name:'{3}',taburl:'{4}',isuse:'{5}',icon:'{6}',order:'{7}'}}", UpAuth.AuthCode, UpAuth.PAuthCode, UpAuth.AuthLevel, UpAuth.AuthName, UpAuth.FileName, UpAuth.IsUse, UpAuth.Ico, UpAuth.DisplayOrder);
                        context.Response.Write("[" + strUpAuthTree + "]");
                    }
                    break;

                case "updateRole"://修改角色Role.aspx
                    {
                        int nodeid = int.Parse(context.Request.QueryString["rolecode"].ToString());

                        JH_Auth_Role UPRole = new JH_Auth_RoleB().GetEntities(d => d.RoleCode == nodeid).SingleOrDefault();
                        UPRole.RoleName = context.Request.QueryString["rolename"].ToString();
                        UPRole.RoleDec = context.Request.QueryString["roledec"].ToString();
                        UPRole.isSysRole = context.Request.QueryString["issys"].ToString();
                        UPRole.IsUse = context.Request.QueryString["isuse"].ToString();
                        new JH_Auth_RoleB().Update(UPRole);
                        context.Response.Write("[success]");
                    }
                    break;
                case "GetRoleMenu"://设置角色权限assginRole.aspx
                    {
                        int RoleCode = int.Parse(context.Request.QueryString["RoleCode"].ToString());
                        string strMenu = "[" + new JH_Auth_RoleAuthB().GetRoleAuth(RoleCode, -1).TrimEnd(',') + "]";
                        context.Response.Write(strMenu);
                    }
                    break;
                case "GetBranchUser"://获取所有机构用户AssignUser.aspx
                    {
                        int RoleCode = int.Parse(context.Request.QueryString["RoleCode"].ToString());
                        string strUserTree = "[" + new JH_Auth_BranchB().GetBranchUserTree(RoleCode, -1).TrimEnd(',') + "]";
                        context.Response.Write(strUserTree);
                    }
                    break;
                case "GetAllAuth"://MenuList.aspx
                    {
                        string strAuth = "[" + new JH_Auth_MenuB().GetAllAuth(-1).TrimEnd(',') + "]";
                        context.Response.Write(strAuth);
                    }
                    break;
                case "GetBranch"://获取所有机构BranchList.aspx
                    {
                        string strBranchTree = "[" + new JH_Auth_BranchB().GetBranchTree(-1).TrimEnd(',') + "]";
                        context.Response.Write(strBranchTree);
                    }
                    break;
                case "GetAllUser"://获取所有用户UserList.aspx
                    {
                        string strUserJson = new JH_Auth_UserB().GetAllUser().TrimEnd(',');
                        context.Response.Write(strUserJson);
                    }
                    break;
                case "delBranch"://获取所有用户UserList.aspx
                    {
                        int intBranchCode = int.Parse(context.Request.QueryString["delCode"].ToString());
                        new JH_Auth_BranchB().DelBranchByCode(intBranchCode);
                        context.Response.Write("success");
                    }
                    break;
                case "DelMenu"://删除菜单
                    {
                        int MenuID = int.Parse(context.Request.QueryString["MenuID"].ToString());
                        if (new JH_Auth_MenuB().DelAllMenuByID(MenuID))
                        {
                            //获取新的权限列表
                            context.Response.Write("success");
                        }
                        else
                        {
                            context.Response.Write("error");
                        }
                    }
                    break;

                case "delUser"://删除用户
                    {
                        string strUserName = context.Request.QueryString["delCode"].ToString();
                        new JH_Auth_UserB().Delete(d => d.UserName == strUserName);
                    }
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {

            context.Response.Write("error");
        }
        ////Branch.aspx
        ////获取所有机构
        //if (strAction == "GetBranch")
        //{
        //    string strUserTree = "[" + new CommonHelp.Account().GetBranchTree(-1).TrimEnd(',') + "]";
        //    context.Response.Write(strUserTree);
        //}

        //if (strAction == "GetAllAuth")
        //{
        //    string strAuth = "[" + new CommonHelp.Account().GetAllAuth(-1).TrimEnd(',') + "]";
        //    context.Response.Write(strAuth);
        //}




        ////删除机构Branch.aspx
        //if (strAction == "delBranch")
        //{
        //    int intBranchCode = int.Parse(context.Request.QueryString["delCode"].ToString());
        //    new CommonHelp.Account().DelBranchByCode(intBranchCode);
        //}
        ////删除用户Branch.aspx
        //if (strAction == "delUser")
        //{
        //    string strUserName = context.Request.QueryString["delCode"].ToString();
        //    new CommonHelp.Account().DelUserByCode(strUserName);
        //}

        ////Menu.aspx
        ////获取菜单管理页面所有菜单Menu.aspx
        //if (strAction == "GetAllAuth")
        //{
        //    string strAuth = "[" + new CommonHelp.Account().GetAllAuth(-1).TrimEnd(',') + "]";
        //    context.Response.Write(strAuth);
        //}
        ////增加菜单Menu.aspx
        //if (strAction == "AddAuth")
        //{
        //    StringBuilder strAddAuthTree = new StringBuilder();
        //    JH_Auth_Menu NewAuth = new JH_Auth_Menu();
        //    NewAuth.PAuthCode = int.Parse(context.Request.QueryString["nodeid"].ToString());
        //    NewAuth.DisplayOrder = int.Parse(context.Request.QueryString["nodeorder"].ToString());
        //    NewAuth.AuthLevel = int.Parse(context.Request.QueryString["nodelever"].ToString()) + 1;
        //    NewAuth.AuthName = context.Request.QueryString["nodename"].ToString();
        //    NewAuth.FileName = context.Request.QueryString["nodeurl"].ToString();
        //    NewAuth.IsUse = context.Request.QueryString["nodeisuse"].ToString();
        //    NewAuth.Ico = context.Request.QueryString["nodeico"].ToString();
        //    NewAuth.isMenu = "Y";
        //    NewAuth.isSys = "N";
        //    NewAuth.AuthDesc = "";
        //    new JH_Auth_MenuB().Insert(NewAuth);
        //    strAddAuthTree.AppendFormat("{{id:'{0}',pId:'{1}',level:'{2}',name:'{3}',taburl:'{4}',isuse:'{5}',icon:'{6}'}}", NewAuth.AuthCode, NewAuth.PAuthCode, NewAuth.AuthLevel, NewAuth.AuthName, NewAuth.FileName, NewAuth.IsUse, NewAuth.Ico);
        //    context.Response.Write("[" + strAddAuthTree + "]");

        //}
        ////添加菜单下的按钮Menu.aspx
        //if (strAction == "AddMenuButton")
        //{
        //    StringBuilder strAddAuthTree = new StringBuilder();
        //    JH_Auth_Menu NewAuth = new JH_Auth_Menu();
        //    NewAuth.PAuthCode = int.Parse(context.Request.QueryString["MenuID"].ToString());
        //    NewAuth.AuthLevel = int.Parse(context.Request.QueryString["nodelever"].ToString()) + 1;
        //    NewAuth.AuthName = context.Request.QueryString["btndec"].ToString();
        //    NewAuth.FileName = context.Request.QueryString["btnid"].ToString();
        //    NewAuth.IsUse = "Y";
        //    NewAuth.Ico = "";
        //    NewAuth.isMenu = "N";
        //    NewAuth.isSys = "N";
        //    NewAuth.AuthDesc = "";
        //    new JH_Auth_MenuB().Insert(NewAuth);
        //    strAddAuthTree.AppendFormat("{{id:'{0}',pId:'{1}',level:'{2}',name:'{3}',taburl:'{4}',isuse:'{5}',icon:'{6}'}}", NewAuth.AuthCode, NewAuth.PAuthCode, NewAuth.AuthLevel, NewAuth.AuthName, NewAuth.FileName, NewAuth.IsUse, NewAuth.Ico);
        //    context.Response.Write("[" + strAddAuthTree + "]");

        //}
        ////修改菜单Menu.aspx
        //if (strAction == "UpdateAuth")
        //{
        //    int nodeid = int.Parse(context.Request.QueryString["nodeid"].ToString());
        //    StringBuilder strUpAuthTree = new StringBuilder();
        //    JH_Auth_Menu UpAuth = new JH_Auth_MenuB().GetEntities(d => d.AuthCode == nodeid).SingleOrDefault();
        //    UpAuth.AuthName = context.Request.QueryString["nodename"].ToString();
        //    UpAuth.DisplayOrder = int.Parse(context.Request.QueryString["nodeorder"].ToString());
        //    UpAuth.FileName = context.Request.QueryString["nodeurl"].ToString();
        //    UpAuth.IsUse = context.Request.QueryString["nodeisuse"].ToString();
        //    UpAuth.Ico = context.Request.QueryString["nodeico"].ToString();
        //    new JH_Auth_MenuB().Update(UpAuth);
        //    strUpAuthTree.AppendFormat("{{id:'{0}',pId:'{1}',level:'{2}',name:'{3}',taburl:'{4}',isuse:'{5}',icon:'{6}',order:'{7}'}}", UpAuth.AuthCode, UpAuth.PAuthCode, UpAuth.AuthLevel, UpAuth.AuthName, UpAuth.FileName, UpAuth.IsUse, UpAuth.Ico, UpAuth.DisplayOrder);
        //    context.Response.Write("[" + strUpAuthTree + "]");
        //}
        ////修改菜单Menu.aspx
        //if (strAction == "UpdateMenuBtn")
        //{

        //    int nodeid = int.Parse(context.Request.QueryString["btncode"].ToString());
        //    StringBuilder strUpAuthTree = new StringBuilder();
        //    JH_Auth_Menu UpAuth = new JH_Auth_MenuB().GetEntities(d => d.AuthCode == nodeid).SingleOrDefault();
        //    UpAuth.AuthName = context.Request.QueryString["btndec"].ToString();
        //    UpAuth.FileName = context.Request.QueryString["btnid"].ToString();
        //    new JH_Auth_MenuB().Update(UpAuth);
        //    strUpAuthTree.AppendFormat("{{id:'{0}',pId:'{1}',level:'{2}',name:'{3}',taburl:'{4}',isuse:'{5}',icon:'{6}',order:'{7}'}}", UpAuth.AuthCode, UpAuth.PAuthCode, UpAuth.AuthLevel, UpAuth.AuthName, UpAuth.FileName, UpAuth.IsUse, UpAuth.Ico, UpAuth.DisplayOrder);
        //    context.Response.Write("[" + strUpAuthTree + "]");
        //}
        ////删除菜单Menu.aspx
        //if (strAction == "delAuth")
        //{
        //    int intAuthCode = int.Parse(context.Request.QueryString["delCode"].ToString());

        //    new CommonHelp.Account().DelAuthCode(intAuthCode);
        //}

        ////Role.aspx
        ////获取所有角色Role.aspx
        //if (strAction == "GetRole")
        //{
        //    string strRoleTree = "[" + new CommonHelp.Account().GetRoleTree(-1).TrimEnd(',') + "]";
        //    context.Response.Write(strRoleTree);
        //}
        ////删除角色Role.aspx
        //if (strAction == "delRole")
        //{
        //    int intRoleCode = int.Parse(context.Request.QueryString["delCode"].ToString());
        //    new CommonHelp.Account().delRole(intRoleCode);
        //}
        ////添加角色Role.aspx
        //if (strAction == "addRole")
        //{
        //    JH_Auth_Role NewRole = new JH_Auth_Role();
        //    NewRole.PRoleCode = 0;
        //    NewRole.RoleName = context.Request.QueryString["rolename"].ToString();
        //    NewRole.RoleDec = context.Request.QueryString["roledec"].ToString();
        //    NewRole.isSysRole = context.Request.QueryString["issys"].ToString();
        //    NewRole.IsUse = context.Request.QueryString["isuse"].ToString();
        //    new JH_Auth_RoleB().Insert(NewRole);
        //    context.Response.Write("success");
        //}

        ////修改角色Role.aspx
        //if (strAction == "updateRole")
        //{
        //    int nodeid = int.Parse(context.Request.QueryString["rolecode"].ToString());

        //    JH_Auth_Role UPRole = new JH_Auth_RoleB().GetEntities(d => d.RoleCode == nodeid).SingleOrDefault();
        //    UPRole.RoleName = context.Request.QueryString["rolename"].ToString();
        //    UPRole.RoleDec = context.Request.QueryString["roledec"].ToString();
        //    UPRole.isSysRole = context.Request.QueryString["issys"].ToString();
        //    UPRole.IsUse = context.Request.QueryString["isuse"].ToString();
        //    new JH_Auth_RoleB().Update(UPRole);
        //    context.Response.Write("[success]");
        //}

        ////设置角色权限assginRole.aspx
        //if (strAction == "GetRoleMenu")
        //{
        //    int RoleCode = int.Parse(context.Request.QueryString["RoleCode"].ToString());
        //    string strMenu = "[" + new CommonHelp.Account().GetRoleAuth(RoleCode, -1).TrimEnd(',') + "]";
        //    context.Response.Write(strMenu);
        //}

        ////AssignUser.ASPX
        ////获取所有机构用户
        //if (strAction == "GetBranchUser")
        //{
        //    int RoleCode = int.Parse(context.Request.QueryString["RoleCode"].ToString());
        //    string strUserTree = "[" + new CommonHelp.Account().GetBranchUserTree(RoleCode, -1).TrimEnd(',') + "]";
        //    context.Response.Write(strUserTree);

        //}

        ////删除用户角色关系
        //if (strAction == "DelUserRole")
        //{
        //    int RoleCode = int.Parse(context.Request.QueryString["rolecode"].ToString());

        //    string UserName = context.Request.QueryString["username"].ToString();
        //    new CommonHelp.Account().DelUserRole(UserName, RoleCode);
        //}

        //if (strAction == "addBranch")
        //{
        //    JH_Auth_Branch NewBranch = new JH_Auth_Branch();
        //    NewBranch.DeptName = context.Request.QueryString["BranchName"].ToString();
        //    NewBranch.DeptRoot = int.Parse(context.Request.QueryString["PBranchCode"].ToString());
        //    NewBranch.Remark1 = context.Request.QueryString["ChargePeople"].ToString();
        //    NewBranch.DeptDesc = NewBranch.DeptName;
        //    NewBranch.BranchLevel = 0;
        //    NewBranch.DeptShort = 0;
        //    new JH_Auth_BranchB().Insert(NewBranch);
        //    context.Response.Write(NewBranch.DeptCode);
        //}

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}